//
//  keyboardView.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/11/6.
//  Copyright © 2015年 szb. All rights reserved.
//

#import "keyboardView.h"
#import "UIColor+Extension.h"
#import "ColorCollectionVC.h"
#import "ColorTableViewCell.h"
#import "ZiHaoTableViewCell.h"

@interface keyboardView ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UIButton *textStyleBtn;
@property (nonatomic, strong) UIButton *textStyleBtnTwo;
@property (nonatomic, strong) UIButton *textStyleBtnThree;

@property (nonatomic, strong) NSArray *imageName;
@property (nonatomic, strong) NSArray *imageOver;

@property (nonatomic, strong) NSArray *imageA;
@property (nonatomic, strong) NSArray *imageAllOver;

@end

@implementation keyboardView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        NSArray *familyNames =[[NSArray alloc]initWithArray:[UIFont familyNames]];
//        NSArray *fontNames;
//        NSInteger indFamily, indFont;
//        NSLog(@"[familyNames count]===%d",[familyNames count]);
//        for(indFamily=0;indFamily<[familyNames count];++indFamily)
//            
//        {
//            NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
//            fontNames =[[NSArray alloc]initWithArray:[UIFont fontNamesForFamilyName:[familyNames objectAtIndex:indFamily]]];
//            
//            for(indFont=0; indFont<[fontNames count]; ++indFont)
//                
//            {
//                NSLog(@"Font name: %@",[fontNames objectAtIndex:indFont]);
//                
//            }  
//        }
        
        
        _keyboardData = [[keyboardData alloc] init];
        _textSize = [NSMutableArray array];
        _textColors = [NSMutableArray array];
        _textFonts = [NSMutableArray array];
        _imageA = [NSArray array];
        _imageAllOver = [NSArray array];
        
        for (keyboardModel *model in _keyboardData.datas) {
            if ([model.textType isEqualToString:@"字体"]) {
                [_textFonts addObject:model];
            }else if ([model.textType isEqualToString:@"颜色"]) {
                [_textColors addObject:model];
            }
            else{
                [_textSize addObject:model];
            }
        }
        _textArry = _textFonts;
        
        self.imageName = [NSArray arrayWithObjects:@"downloadText",@"textColor",@"textFont", nil];
        self.imageOver = [NSArray arrayWithObjects:@"downloadText-over",@"textColor-over",@"textFont-over", nil];
        
        self.imageA = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22", nil];
        self.imageAllOver = [NSArray arrayWithObjects:@"1-1",@"2-1",@"3-1",@"4-1",@"5-1",@"6-1",@"7-1",@"8-1",@"9-1",@"10-1",@"11-1",@"12-1",@"13-1",@"14-1",@"15-1",@"16-1",@"17-1",@"18-1",@"19-1",@"20-1",@"21-1",@"22-1", nil];
        _ID = @"cellID";
        _keyboardTable = [[UITableView alloc] initWithFrame:self.bounds];
        _keyboardTable.delegate = self;
        _keyboardTable.dataSource = self;
        _keyboardTable.backgroundColor = [UIColor lightGrayColor];
        _keyboardTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        [self addSubview:_keyboardTable];
        
        
        
        CGFloat textH = 45.0f;
        UIView *textView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, BPScreenW, textH)];
        _keyboardTable.tableHeaderView = textView;
        BPLineView *lineView = [[BPLineView alloc] initWithFrame:CGRectMake(0, textH - 1, BPScreenW, 1)];
        [textView addSubview:lineView];
        CGFloat btnW = BPScreenW / 3.0;
//        for (int i = 0; i < 3; i ++) {
//            UIButton *textStyleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//            textStyleBtn.frame = CGRectMake(btnW * i, 0, btnW, textH);
//            textStyleBtn.tag = i+10;
//            [textStyleBtn setImage:[UIImage imageNamed:imageName[i]] forState:UIControlStateNormal];
//            [textStyleBtn setTitle:name[i] forState:UIControlStateNormal];
//            textStyleBtn.titleLabel.font = [UIFont systemFontOfSize:13.0f];
//            [textStyleBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
//            [textView addSubview:textStyleBtn];
//            [textStyleBtn addTarget:self action:@selector(textBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//        }
        
        self.textStyleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.textStyleBtn.frame = CGRectMake(0, 0, btnW, textH);
        self.textStyleBtn.tag = 10;
        [self.textStyleBtn setImage:[UIImage imageNamed:@"downloadText-over"] forState:UIControlStateNormal];
        //[self.textStyleBtn setTitle:name[0] forState:UIControlStateNormal];
        self.textStyleBtn.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        [self.textStyleBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        [textView addSubview:self.textStyleBtn];
        [self.textStyleBtn addTarget:self action:@selector(textBtnClick:) forControlEvents:UIControlEventTouchUpInside];

        
        
        self.textStyleBtnTwo = [UIButton buttonWithType:UIButtonTypeCustom];
        self.textStyleBtnTwo.frame = CGRectMake(btnW, 0, btnW, textH);
        self.textStyleBtnTwo.tag = 11;
        [self.textStyleBtnTwo setImage:[UIImage imageNamed:self.imageName[1]] forState:UIControlStateNormal];
        //[self.textStyleBtnTwo setTitle:name[1] forState:UIControlStateNormal];
        self.textStyleBtnTwo.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        [self.textStyleBtnTwo setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        [textView addSubview:self.textStyleBtnTwo];
        [self.textStyleBtnTwo addTarget:self action:@selector(textBtnClick:) forControlEvents:UIControlEventTouchUpInside];

        
        
        self.textStyleBtnThree = [UIButton buttonWithType:UIButtonTypeCustom];
        self.textStyleBtnThree.frame = CGRectMake(btnW * 2, 0, btnW, textH);
        self.textStyleBtnThree.tag = 12;
        [self.textStyleBtnThree setImage:[UIImage imageNamed:self.imageName[2]] forState:UIControlStateNormal];
        //[self.textStyleBtnThree setTitle:name[2] forState:UIControlStateNormal];
        self.textStyleBtnThree.titleLabel.font = [UIFont systemFontOfSize:13.0f];
        [self.textStyleBtnThree setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        [textView addSubview:self.textStyleBtnThree];
        [self.textStyleBtnThree addTarget:self action:@selector(textBtnClick:) forControlEvents:UIControlEventTouchUpInside];

        
    }
    return self;
}

- (void)textBtnClick:(UIButton *)button
{
    switch (button.tag) {
        case 10:
            [self.textStyleBtnTwo setImage:[UIImage imageNamed:self.imageName[1]] forState:UIControlStateNormal];
            [self.textStyleBtnThree setImage:[UIImage imageNamed:self.imageName[2]] forState:UIControlStateNormal];
            [self.textStyleBtn setImage:[UIImage imageNamed:self.imageOver[0]] forState:UIControlStateNormal];
            
            _textArry = _textFonts;
            _arryType = 0;
            [_keyboardTable reloadData];
            break;
            
        case 11:
            
            [self.textStyleBtn setImage:[UIImage imageNamed:self.imageName[0]] forState:UIControlStateNormal];
            [self.textStyleBtnThree setImage:[UIImage imageNamed:self.imageName[2]] forState:UIControlStateNormal];
            [self.textStyleBtnTwo setImage:[UIImage imageNamed:self.imageOver[1]] forState:UIControlStateNormal];
            
            _textArry = _textColors;
            _arryType = 1;
            [_keyboardTable reloadData];
            break;
            
        case 12:
            
            [self.textStyleBtnThree setImage:[UIImage imageNamed:self.imageOver[2]] forState:UIControlStateNormal];
            [self.textStyleBtn setImage:[UIImage imageNamed:self.imageName[0]] forState:UIControlStateNormal];
            [self.textStyleBtnTwo setImage:[UIImage imageNamed:self.imageName[1]] forState:UIControlStateNormal];
            
            _textArry = _textSize;
            _arryType = 2;
            [_keyboardTable reloadData];
            break;
            
        default:
            break;
    }
}



#pragma mark - 键盘tableview代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_arryType == 0 || _arryType == 2) {
        
        return [_textArry count];
    }else{
        return 4;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_arryType == 0) {

        textStyleCell *cell = [textStyleCell cellWithTableView:tableView];
        [cell setCellSubviewsNil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        keyboardModel *model = _textArry[indexPath.row];
//        cell.textLabel.text = @"字体样式";
//        cell.textLabel.textColor = [UIColor blackColor];
//        cell.textLabel.font = [UIFont fontWithName:model.textFont size:14.0f];
    
        if (self.temp - 1 == indexPath.row) {
            cell.duiHao.hidden = NO;
        }else{
            cell.duiHao.hidden = YES;
        }

        cell.label.text = @"天天美印";
        cell.label.textColor = [UIColor blackColor];
        cell.label.font = [UIFont fontWithName:model.textFont size:14.0f];
        cell.Jian.image = [UIImage imageNamed:@"Jian"];
        
        return cell;
       
    }else if (_arryType == 2)
    {
        ZiHaoTableViewCell *cell = [ZiHaoTableViewCell cellZiHaoWithTableView:tableView];
        [cell setCellZiHaoSubviewsNil];
        
        if (self.tempLast - 1 == indexPath.row) {
            cell.duiHaoS.hidden = NO;
        }else{
            cell.duiHaoS.hidden = YES;
        }

        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        keyboardModel *model = _textArry[indexPath.row];
        cell.textLabel.text = @"天天美印";
        cell.textLabel.font = [UIFont systemFontOfSize:model.textSize - 4.0];
        //cell.textLabel.text = [NSString stringWithFormat:@"%.0f号字体",model.textSize];
        cell.fontSize = indexPath.row;
        return  cell;
    }
    else{
        ColorTableViewCell *cell = [ColorTableViewCell cellColorWithTableView:tableView];
        [cell setCellColorSubviewsNil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
            if (indexPath.row == 0) {
                
                [cell.a setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
                cell.a.tag =  1;
                [cell.a addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                
                [cell.b setImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal];
                cell.b.tag = 2;
                [cell.b addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                
                [cell.c setImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
                cell.c.tag = 3;
                [cell.c addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                
                [cell.d setImage:[UIImage imageNamed:@"4"] forState:UIControlStateNormal];
                cell.d.tag = 4;
                [cell.d addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                
                [cell.e setImage:[UIImage imageNamed:@"5"] forState:UIControlStateNormal];
                cell.e.tag = 5;
                [cell.e addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                
                [cell.f setImage:[UIImage imageNamed:@"6"] forState:UIControlStateNormal];
                cell.f.tag = 6;
                [cell.f addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
            }
            if (indexPath.row == 2) {
                [cell.a setImage:[UIImage imageNamed:@"12"] forState:UIControlStateNormal];
                cell.a.tag =  12;
                [cell.a addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                
                [cell.b setImage:[UIImage imageNamed:@"13"] forState:UIControlStateNormal];
                cell.b.tag = 13;
                [cell.b addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                
                [cell.c setImage:[UIImage imageNamed:@"14"] forState:UIControlStateNormal];
                cell.c.tag = 14;
                [cell.c addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                
                [cell.d setImage:[UIImage imageNamed:@"15"] forState:UIControlStateNormal];
                cell.d.tag = 15;
                [cell.d addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                
                [cell.e setImage:[UIImage imageNamed:@"16"] forState:UIControlStateNormal];
                cell.e.tag = 16;
                [cell.e addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                
                [cell.f setImage:[UIImage imageNamed:@"17"] forState:UIControlStateNormal];
                cell.f.tag = 17;
                [cell.f addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];

            }
            
       
            if (indexPath.row == 1) {
                
                [cell.aa setImage:[UIImage imageNamed:@"7"] forState:UIControlStateNormal];
                cell.aa.tag = 7;
                [cell.aa addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                [cell.bb setImage:[UIImage imageNamed:@"8"] forState:UIControlStateNormal];
                cell.bb.tag = 8;
                [cell.bb addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                [cell.cc setImage:[UIImage imageNamed:@"9"] forState:UIControlStateNormal];
                cell.cc.tag = 9;
                [cell.cc addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                [cell.dd setImage:[UIImage imageNamed:@"10"] forState:UIControlStateNormal];
                cell.dd.tag = 10;
                [cell.dd addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                [cell.ee setImage:[UIImage imageNamed:@"11"] forState:UIControlStateNormal];
                cell.ee.tag = 11;
                [cell.ee addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
            }
            if (indexPath.row == 3) {
                
                [cell.aa setImage:[UIImage imageNamed:@"18"] forState:UIControlStateNormal];
                cell.aa.tag = 18;
                [cell.aa addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                [cell.bb setImage:[UIImage imageNamed:@"19"] forState:UIControlStateNormal];
                cell.bb.tag = 19;
                [cell.bb addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                [cell.cc setImage:[UIImage imageNamed:@"20"] forState:UIControlStateNormal];
                cell.cc.tag = 20;
                [cell.cc addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                [cell.dd setImage:[UIImage imageNamed:@"21"] forState:UIControlStateNormal];
                cell.dd.tag = 21;
                [cell.dd addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
                [cell.ee setImage:[UIImage imageNamed:@"22"] forState:UIControlStateNormal];
                cell.ee.tag = 22;
                [cell.ee addTarget:self action:@selector(a:) forControlEvents:UIControlEventTouchUpInside];
                
            }
          
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_arryType == 0) {
        self.temp = indexPath.row + 1;
        if ([self.delegate respondsToSelector:@selector(didClickCell: with:)]) {
            
            for (int i = 0; i < _textArry.count; i++) {
                NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
                textStyleCell *cell = [self.keyboardTable cellForRowAtIndexPath:path];
                
                if (indexPath.row == i) {
                    cell.duiHao.hidden = NO;
                }else{
                    cell.duiHao.hidden = YES;
                }
//                if (i != indexPath.row) {
//                    cell.duiHao.hidden = YES;
//                }else{
//                    cell.duiHao.hidden = NO;
//                }
            }
            
            [self.delegate didClickCell:_textArry[indexPath.row] with:indexPath.row];
        }
    }
    if (_arryType == 2) {
        self.tempLast = indexPath.row + 1;
        if ([self.delegate respondsToSelector:@selector(didClickCell: with:)]) {
            
            for (int i = 0; i < _textArry.count; i++) {
                NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
                ZiHaoTableViewCell *cell = [self.keyboardTable cellForRowAtIndexPath:path];
                if (indexPath.row == i) {
                    cell.duiHaoS.hidden = NO;
                }else{
                    cell.duiHaoS.hidden = YES;
                }
            }
            [self.delegate didClickCell:_textArry[indexPath.row] with:indexPath.row];
        }
    }

}



- (void)a:(UIButton *)buA{
   
    if ([self.delegate respondsToSelector:@selector(didClickCell: with:)]) {
        
        for (int i = 0; i < 22; i++) {
            
            UIButton *but = (UIButton *)[self viewWithTag:i+ 1];
            if (but.tag == buA.tag) {
                
                [but setImage:[UIImage imageNamed:self.imageAllOver[i]] forState:UIControlStateNormal];
                [self.delegate didClickCell:_textArry[i] with:1];
                
                
            }else{
                [but setImage:[UIImage imageNamed:self.imageA[i]] forState:UIControlStateNormal];
                //[self.delegate didClickCell:_textArry[i] with:1];
            }
        }
    }
}



@end
